package com.db.gffxo.epi.repository;


import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.simple.ParameterizedRowMapper;
import org.springframework.jdbc.core.simple.SimpleJdbcDaoSupport;
import com.db.gffxo.epi.domain.Event;


public class EventDaoGen extends SimpleJdbcDaoSupport implements EventDao {

private final static String QUERY = "SELECT * FROM Event";
private final static String REMOVEBYEventId = "DELETE FROM Event WHERE EventId = ?";
private final static String QUERYBYEventId = "SELECT * FROM Event WHERE EventId = ?";
private final static String INSERT = "INSERT INTO Event(EventId, EventType, Status, KeyInfo ) VALUES (?,?,?,?)";
private final static String UPDATEStatus = "UPDATE Event set status=? where eventId=?";
private final static String QUERYbyStatusType="SELECT * FROM Event where status=? and EVENTTYPE in ( OURTYPES ) and rownum < ?";


	public void removeByEventId(Integer EventId) {
		getSimpleJdbcTemplate().update(REMOVEBYEventId, EventId);
	}


	public void insert(Event event) {
		getSimpleJdbcTemplate().update(INSERT
			,event.getEventId()
			,event.getEventType()
			,event.getStatus()
			,event.getKeyInfo()
		);
	}

	public List getEvent() {
		List events = getSimpleJdbcTemplate().query(QUERY,
			new RowMapper<Event>() {
				public Event mapRow(ResultSet rs, int rowNum) throws SQLException {
				Event event = new Event();
				event.setEventId(rs.getInt(1));
				event.setEventType(rs.getString(2));
				event.setStatus(rs.getString(3));
				event.setKeyInfo(rs.getString(4));
				return event;
			}
		});
		return events;
	}
	
	public List getEventbyStatusType(String status,String type, int rows) {
		String sqlString = QUERYbyStatusType.replaceFirst("OURTYPES", type);
		List events = getSimpleJdbcTemplate().query(sqlString,
			new RowMapper<Event>() {
				public Event mapRow(ResultSet rs, int rowNum) throws SQLException {
				Event event = new Event();
				event.setEventId(rs.getInt(1));
				event.setEventType(rs.getString(2));
				event.setStatus(rs.getString(3));
				event.setKeyInfo(rs.getString(4));
				return event;
			}
			}, status, rows);
		return events;
	}
	
	public List getEventbyEventId(Integer EventId) {
		List events = getSimpleJdbcTemplate().query(QUERYBYEventId,
			new ParameterizedRowMapper() {
				public Event mapRow(ResultSet rs, int rowNum) throws SQLException {
				Event event = new Event();
				event.setEventId(rs.getInt(1));
				event.setEventType(rs.getString(2));
				event.setStatus(rs.getString(3));
				event.setKeyInfo(rs.getString(4));
				return event;
			}
		}
		,EventId);
		return events;
	}


	public List getNextBatch(String status, String Types, int size) {
		// TODO Auto-generated method stub
		return null;
	}


	public void updateStatus(Event event) {
		getSimpleJdbcTemplate().update(UPDATEStatus
			,event.getStatus()
			,event.getEventId()
		);
	}

}